package com.ztc.controller;
import com.ztc.beans.Book;
import com.ztc.beans.Hobby;
import com.ztc.beans.Role;
import com.ztc.beans.User;
import com.ztc.service.UserService;
import com.ztc.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/User")
public class UserController {

    @Autowired
    private UserService userService;

   /* @RequestMapping("/getuser")
    public String getUser(Model model){

        List<User> uList = userService.getUser();
        model.addAttribute("uList",uList);
        return "userlist";
    }*/
    @RequestMapping("/login")
    public String login(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = userService.Login(request.getParameter("username"),request.getParameter("password"));
        if (user!=null) {
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            List<Book> blist = userService.getbook();
            request.setAttribute("list", blist);
            if (user.getRid()==1) {
               return "redirect:/Book/inquire";
            } else {
                /*try {
                    request.getRequestDispatcher("userlist").forward(request, response);
                } catch (ServletException e) {
                    e.printStackTrace();
                }*/
                return "userlist";
            }
        }else{
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.println("登录失败！！！");
        }

        return "index";
    }
    @RequestMapping("/getrole")
    @ResponseBody
    public String getrole(){
        List<Role> rlist = userService.getroles();
        String roleJson = JSON.toJSONString(rlist);
        return roleJson;
    }
    @RequestMapping("/inquire")
    public String inquire(HttpServletRequest request){
        Page page = new Page<>();
        //获取当前页
        String pagesize =request.getParameter("pagesize")==null?"1":request.getParameter("pagesize");
        page.setCurrentPage(Integer.parseInt(pagesize));
        String mname = request.getParameter("mname")==null?"":request.getParameter("mname");
        String mage = request.getParameter("mage");
        String msex = request.getParameter("msex")==null?"":request.getParameter("msex");
        String mrid = request.getParameter("mrid")==null?"":request.getParameter("mrid");
        List<User> ulist = userService.getusers2(mname,mage,msex,mrid,page);

        int sum = userService.getUserSum(mname,mage,msex,mrid);
        page.setRecordTotal(sum);
        page.setContent(ulist);
        System.out.println(ulist);
        if(!ulist.isEmpty()){
            request.setAttribute("page",page);
            request.setAttribute("mname",mname);
            request.setAttribute("mage",mage);
            request.setAttribute("msex",msex);
            request.setAttribute("mrid",mrid);
        }else{
          /*  List<User>  u2list = userService.getusers(page);
            page.setContent(u2list);
            page.setRecordTotal(3);*/
            request.setAttribute("msg","数据为空");
           /* return "redirect:/User/inquire";*/
        }
       return "adminuser";
    }

    @RequestMapping("/register")
    private String zcadduser(HttpServletRequest request) {
        User user = new User();
        String username = request.getParameter("username");
        if (username == "" || username == null) {
            request.setAttribute("msg", "账号或密码不能为空！！");
        } else {
            //判断账号是否重复
            if (userService.register(username) != null) {
                request.setAttribute("msg", "账号重复");
            } else {
                /* user.setName(request.getParameter("name"));*/
                user.setUsername(request.getParameter("username"));
                user.setPassword(request.getParameter("pwd"));
            /*user.setSex(request.getParameter("sex"));
            user.setAge(Integer.parseInt(request.getParameter("age")));
            String [] hobby =request.getParameterValues("hobby");
            user.setHobby(String.join(",",hobby));*/
                userService.zcadduser2(user);
                request.setAttribute("msg", "注册成功");
            }
        }
        return "index";
    }
    @RequestMapping("/zcuser")
    private String zcuser(HttpServletRequest request) {
        List<Hobby> hlist = userService.gethobbys();
        request.setAttribute("hobby",hlist);
        return "zcadduser";

    }
    @RequestMapping("/deuser")
    private String deuser(@RequestParam("id") String id) {
        userService.deUser(Integer.parseInt(id));
        return "redirect:/User/inquire";
    }
    @RequestMapping("/updateuser")
    private void updateuser(HttpServletRequest request) {
        String id= request.getParameter("id");
        User user1 = userService.getUserId(id);
        User user = new User();
        user.setId(Integer.parseInt(id));
        //判断账号是否重复
        user.setUsername(request.getParameter("username"));
        if(user1.getUsername().equals(user.getUsername())){
            user.setName(request.getParameter("name"));
            user.setPassword(request.getParameter("pwd"));
            user.setSex(request.getParameter("sex"));
            user.setAge(Integer.parseInt(request.getParameter("age")));
            user.setRid(Integer.parseInt(request.getParameter("rid")));
            System.out.println(user.getRid());
            //爱好多选框加入数据库处理
            String [] hobby =request.getParameterValues("hobby");
            user.setHobby(String.join(",",hobby));
            userService.updateuser(user);
            request.setAttribute("msg","修改成功");
        }else{
            boolean b = userService.getusername(user.getUsername());
            if(b&&!user1.getUsername().equals(request.getParameter("username"))){
                request.setAttribute("msg","修改的账号冲突！！！");
            }else{
                user.setName(request.getParameter("name"));
                user.setPassword(request.getParameter("pwd"));
                user.setSex(request.getParameter("sex"));
                user.setAge(Integer.parseInt(request.getParameter("age")));
                user.setRid(Integer.parseInt(request.getParameter("rid")));
                System.out.println(user.getRid());
                String [] hobby =request.getParameterValues("hobby");
                user.setHobby(String.join(",",hobby));
                userService.updateuser(user);
                request.setAttribute("msg","修改成功");
            }
        }
        inquire(request);
    }
    @RequestMapping("/upuser")
    private String upuser(@RequestParam("id") String id, HttpServletRequest request) {
        User user = userService.getUserId(id);
        request.setAttribute("user", user);
        List<Role> rlist = userService.getroles();
        List<Hobby> hlist = userService.gethobbys();
        request.setAttribute("role", rlist);
        request.setAttribute("hobby", hlist);
        return "upuser";
    }

    @RequestMapping("/adduser")
    private String adduser(HttpServletRequest request) {
        User user = new User();
        String username = request.getParameter("username");
        //判断账号是否重复
        if(username==""||username==null){
            request.setAttribute("msg","账号或密码不能为空！！");
        }else{
            if(userService.register(username)!=null){
                request.setAttribute("msg","账号重复");

            }else{
                user.setName(request.getParameter("name"));
                user.setUsername(request.getParameter("username"));
                user.setPassword(request.getParameter("pwd"));
                user.setSex(request.getParameter("sex"));
                user.setAge(Integer.parseInt(request.getParameter("age")));
                user.setRid(Integer.parseInt(request.getParameter("rid")));
                System.out.println(user.getRid());
                String [] hobby =request.getParameterValues("hobby");
                user.setHobby(String.join(",",hobby));
                userService.adduser(user);
                System.out.println(user);
                request.setAttribute("msg","添加成功");
            }
        }
       return "redirect:/User/inquire";
    }
    @RequestMapping("/getrole1")
    private String getrole1(HttpServletRequest request, HttpServletResponse response) {
        List<Role> rlist = userService.getroles();
        List<Hobby> hlist = userService.gethobbys();
        request.setAttribute("role",rlist);
        request.setAttribute("hobby",hlist);
        return "adduser";
    }



}
